﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model.Query;
using HZ.Supervise.IBLL;

namespace HZ.Supervise.Web.Hospital
{
    //public partial class PurchaseSummary : System.Web.UI.Page
    //{
        public partial class PurchaseSummary : BasePage
{
        HZ.Supervise.IBLL.IExecuteContext i = HZ.Supervise.BLLFactory.ExecuteContext();
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!IsPostBack)
            {
                ddlArea.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;
            }
		}
        /// <summary>
        /// 根据查询条件，绑定相应的信息
        /// </summary>
        private void SetBind()
        {
            int totalCount = 0;
            DataTable dt = i.GetAllPurchaseSummary(GetQurey(), this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwPurchaseSummary.PageSize = AspNetPager1.PageSize;
            gvwPurchaseSummary.DataSource = dt;
            gvwPurchaseSummary.DataBind();
            DataTable dt1 = i.GetAllPurchaseSummaryTotal(GetQurey());
            DataTable dt2 = i.GetAllPurchaseSummaryTotal2(GetQurey());
            DataTable dt3 = i.GetAllPurchaseSummaryTotal3(GetQurey());
            DataTable dt4 = i.GetAllPurchaseSummaryTotal4(GetQurey());
            this.lblResout.Text = "医疗机构总数：" + dt1.Rows[0]["zs"] + "，&nbsp; 三级医疗机构数：" + dt2.Rows[0]["sj"]
                    + "，&nbsp; 二级医疗机构数：" + dt3.Rows[0]["ej"] + "，&nbsp; 二级以下医疗机构数：" + dt4.Rows[0]["ejyx"];
        }

        private QGoodsDXSB GetQurey()
        {
            QGoodsDXSB qd = new QGoodsDXSB();
            qd.OrderDetailState = 1;
            qd.AreaID = ddlArea.SelectedAreaID;
            if (!string.IsNullOrEmpty(txtTimeFrom.Text.Trim()) && string.IsNullOrEmpty(txtTimeTo.Text.Trim()))
            {
                qd.Append += string.Format("SubmiTime >= '{0}'", txtTimeFrom.Text.Trim());
            }
            else if (!string.IsNullOrEmpty(txtTimeFrom.Text.Trim()) && !string.IsNullOrEmpty(txtTimeTo.Text.Trim()))
            {
                qd.Append += string.Format("SubmiTime >= '{0}' and SubmiTime <= '{1}'", txtTimeFrom.Text.Trim(), txtTimeTo.Text.Trim());
            }
            else if (string.IsNullOrEmpty(txtTimeFrom.Text.Trim()) && !string.IsNullOrEmpty(txtTimeTo.Text.Trim()))
            {
                qd.Append += string.Format("SubmiTime <= '{0}'", txtTimeTo.Text.Trim());
            }
            return qd;
        }
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetBind();
        }

        protected void btnButton_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            int totalrecord = 0;
            DataTable dt = i.GetAllPurchaseSummary(GetQurey(), 1, 0, out totalrecord);
            if (dt.Rows.Count > 0)
            {
                ExportFile.Instance.ExportData(dt, gvwPurchaseSummary, this.Page);
                HZ.Web.WebUtil.Alert("导出成功！", this.Page);
            }
            else
            {
                HZ.Web.WebUtil.Alert("暂无导出的数据！", this.Page);
            }

        }
	}
}